#!/usr/bin/env python3

# Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this
# file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""This script generates `macro_inl.h`"""

import sys
import os

MAX_SEQ_SIZE = 64
DEST_FILE = os.path.abspath(os.path.dirname(sys.argv[0])) + '/macro_inl.h'


def main():
    body = ''

    # FLARE_INTERNAL_PP_SEQ_AT
    body += '#define FLARE_INTERNAL_PP_SEQ_AT_0_IMPL(_0,  ...) _0\n'
    for i in range(1, MAX_SEQ_SIZE):
        args = ['_' + str(x) for x in range(i + 1)]
        result = '#define FLARE_INTERNAL_PP_SEQ_AT_{}_IMPL'.format(i)
        result += '(' + ', '.join(args) + ', ...) _' + str(i)
        body += result + '\n'

    # FLARE_INTERNAL_PP_FOR_EACH
    for i in range(1, MAX_SEQ_SIZE):
        args = ['_' + str(x) for x in range(i)]
        impls = ['Callback(_' + str(x) + ', Context)' for x in range(i)]
        result = '#define FLARE_INTERNAL_PP_FOR_EACH_'
        result += '{}(Callback, Context, {})'.format(i, ', '.join(args))
        result += ' '.join(impls)
        body += result + '\n'

    output = '''// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// Generated by `macro_inl_gen.py`. Don't modify this file by hand.
'''
    output += '\n'
    output += '#ifndef FLARE_BASE_INTERNAL_MACRO_INL_H_\n'
    output += '#define FLARE_BASE_INTERNAL_MACRO_INL_H_\n'
    output += '\n'
    # Every line is marked as `NOLINT`... I'm not sure we want check this
    # auto-generated output for coding-style.
    output += '\n'.join([x + '  // NOLINT' for x in body.split('\n')]) + '\n'
    output += '\n'
    output += '#endif  // FLARE_BASE_INTERNAL_MACRO_INL_H_\n'

    with open(DEST_FILE, 'w') as file:
        file.write(output)


if __name__ == '__main__':
    main()
